package com.lbb.mall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lbb.mall.product.entity.AttrEntity;
import com.lbb.mall.product.service.AttrAttrgroupRelationService;
import com.lbb.mall.product.service.AttrService;
import com.lbb.mall.product.service.CategoryService;
import com.lbb.mall.product.vo.AttrGroupRelationVo;
import com.lbb.mall.product.vo.AttrGroupWithattrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lbb.mall.product.entity.AttrGroupEntity;
import com.lbb.mall.product.service.AttrGroupService;
import com.lbb.common.utils.PageUtils;
import com.lbb.common.utils.R;



/**
 * 属性分组
 *
 * @author lbb
 * @email liubin13218570325@qq.com
 * @date 2021-08-12 17:56:35
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 17、获取分类下所有分组&关联属性
     * /product/attrgroup/{catelogId}/withattr
     * */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithattr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithattrVo> vos = attrGroupService.getAttrGroupWithattrByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    /**
     * 添加属性与分组关联关系
     * /product/attrgroup/attr/relation
     * */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveRelationBatch(vos);
        return R.ok();
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     * /product/attrgroup/{attrgroupId}/noattr/relation
     * */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
       PageUtils page = attrService.getNoRelationAttr(attrgroupId,params);
       return R.ok().put("page",page);
    }

    /**
     * 查询关联属性
     * /product/attrgroup/{attrgroupId}/attr/relation
     * */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entityList = attrService.getRelationAttr(attrgroupId);
        if(entityList != null){
            return R.ok().put("data",entityList);
        }else{
            return R.ok().put("data",new ArrayList<AttrEntity>());
        }
    }

    /**
     * 删除属性与分组的关联关系
     *  /product/attrgroup/attr/relation/delete
     * */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        attrService.deleteRelation(attrGroupRelationVo);
        return R.ok();
    }


    /**
     * 列表
     * @param catelogId  等于0是查询所有
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //用当前的分类id查询完整路径填入attrGroup
        attrGroup.setCatelogPath(categoryService.findCateLogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
